package com.ruoyi.project.jiaqin.supplyclass.controller;

import java.util.Date;
import java.util.List;

import com.ruoyi.framework.web.domain.ZtreeStr;
import com.ruoyi.project.jiaqin.quotationclass.domain.QuotationClass;
import com.ruoyi.project.jiaqin.quotationclass.utils.PinYinUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.jiaqin.supplyclass.domain.SupplyClass;
import com.ruoyi.project.jiaqin.supplyclass.service.ISupplyClassService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 供需类型Controller
 *
 * @author admin
 * @date 2021-01-28
 */
@Controller
@RequestMapping("/jiaqin/supplyclass")
public class SupplyClassController extends BaseController
{
    private String prefix = "jiaqin/supplyclass";

    @Autowired
    private ISupplyClassService supplyClassService;

    @RequiresPermissions("jiaqin:supplyclass:view")
    @GetMapping()
    public String supplyclass()
    {
        return prefix + "/supplyclass";
    }


    /**
     * 去选择分类树页
     *
     * @param deptId 部门ID
     * @param excludeId 排除ID
     */
    @GetMapping(value = { "/selectSupplyClassTree/{supplyClassId}", "/selectSupplyClassTree/{supplyClassId}/{excludeId}" })
    public String selectDeptTree(@PathVariable("supplyClassId") String supplyClassId,
                                 @PathVariable(value = "excludeId", required = false) String excludeId, ModelMap mmap)
    {
        mmap.put("supplyClass", supplyClassService.selectSupplyClassById(supplyClassId));//查分类名
        mmap.put("excludeId", excludeId);
        return prefix + "/tree";
    }

    /**
     * 加载分类列表树
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<ZtreeStr> treeData() {
        List<ZtreeStr> ztrees = supplyClassService.selectSupplyClassTree();
        return ztrees;
    }



    /**
     * 查询一级分类列表
     */
    @RequiresPermissions("jiaqin:supplyclass:list")
    @PostMapping("/listOne")
    @ResponseBody
    public TableDataInfo list(SupplyClass supplyClass)
    {
        supplyClass.setLevel(0);
        startPage();
        List<SupplyClass> list = supplyClassService.selectSupplyClassList(supplyClass);
        return getDataTable(list);
    }



    /**
     * 查询二级分类列表
     */
    @RequiresPermissions("jiaqin:supplyclass:list")
    @PostMapping("/listTwo")
    @ResponseBody
    public TableDataInfo listTwo(SupplyClass supplyClass)
    {
        startPage();
        List<SupplyClass> list = supplyClassService.selectSupplyClassList(supplyClass);
        return getDataTable(list);
    }

    /**
     * 查询二级分类列表
     */
    @RequiresPermissions("jiaqin:supplyclass:list")
    @PostMapping("/listThr")
    @ResponseBody
    public TableDataInfo listThr(SupplyClass supplyClass)
    {
        startPage();
        List<SupplyClass> list = supplyClassService.selectSupplyClassList(supplyClass);
        return getDataTable(list);
    }


    /**
     * 导出供需类型列表
     */
    @RequiresPermissions("jiaqin:supplyclass:export")
    @Log(title = "供需类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SupplyClass supplyClass)
    {
        List<SupplyClass> list = supplyClassService.selectSupplyClassList(supplyClass);
        ExcelUtil<SupplyClass> util = new ExcelUtil<SupplyClass>(SupplyClass.class);
        return util.exportExcel(list, "supplyclass");
    }

    /**
     * 去添加一级分类页
     */
    @GetMapping("/addOne")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 保存一级分类
     */
    @RequiresPermissions("jiaqin:supplyclass:add")
    @Log(title = "供需类型", businessType = BusinessType.INSERT)
    @PostMapping("/addOne")
    @ResponseBody
    public AjaxResult addSave(SupplyClass supplyClass)
    {
        supplyClass.setLevel(0);
        supplyClass.setCreateAt(new Date());
        supplyClass.setParentId("0");
        supplyClass.setInitials(PinYinUtils.hanziToPinyin(supplyClass.getName(),"").substring(0,1));
        return toAjax(supplyClassService.insertSupplyClass(supplyClass));
    }









    /**
     * 去二级分类列表页
     * @param oneId
     * @param model
     * @return
     */
    @RequiresPermissions("jiaqin:supplyclass:list")
    @GetMapping("/toTwo/{oneId}")
    public String toTwo(@PathVariable("oneId") String oneId, Model model) {
        //根据一级分类ID查询二级分类列表
        model.addAttribute("oneId",oneId);
        return prefix + "/supplyclasstwo";
    }
    /**
     * 去二级分类新增页
     */
    @GetMapping("/addTwo/{oneId}")
    public String addTwo(@PathVariable("oneId") String oneId ,Model model)
    {
        model.addAttribute("oneId",oneId);
        return prefix + "/addTwo";
    }
    /**
     * 添加二级分类接口
     */
    @RequiresPermissions("jiaqin:supplyclass:add")
    @Log(title = "行情类型管理", businessType = BusinessType.INSERT)
    @PostMapping("/addTwo")
    @ResponseBody
    public AjaxResult addTwoSave(SupplyClass supplyClass)
    {
        supplyClass.setCreateAt(new Date());
        supplyClass.setLevel(1);
        supplyClass.setInitials(PinYinUtils.hanziToPinyin(supplyClass.getName(),"").substring(0,1));
        return toAjax(supplyClassService.insertSupplyClass(supplyClass));
    }


    /**
     * 去三级分类列表页
     * @param oneId
     * @param model
     * @return
     */
    @RequiresPermissions("jiaqin:supplyclass:list")
    @GetMapping("/toThr/{twoId}")
    public String toThr(@PathVariable("twoId") String twoId, Model model) {
        //根据二级分类ID查询三级分类列表
        model.addAttribute("twoId",twoId);
        return prefix + "/supplyclassthr";
    }
    /**
     * 去三级分类新增页
     */
    @GetMapping("/addThr/{twoId}")
    public String addThr(@PathVariable("twoId") String twoId ,Model model)
    {
        model.addAttribute("twoId",twoId);
        return prefix + "/addThr";
    }
    /**
     * 添加三级分类接口
     */
    @RequiresPermissions("jiaqin:supplyclass:add")
    @Log(title = "行情类型管理", businessType = BusinessType.INSERT)
    @PostMapping("/addThr")
    @ResponseBody
    public AjaxResult addThrSave(SupplyClass supplyClass)
    {
        supplyClass.setCreateAt(new Date());
        supplyClass.setLevel(2);
        supplyClass.setInitials(PinYinUtils.hanziToPinyin(supplyClass.getName(),"").substring(0,1));
        return toAjax(supplyClassService.insertSupplyClass(supplyClass));
    }






    /**
     * 修改供需类型
     */
    @GetMapping("/editOne/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        SupplyClass supplyClass = supplyClassService.selectSupplyClassById(id);
        mmap.put("supplyClass", supplyClass);
        return prefix + "/edit";
    }
    /**
     * 修改供需类型
     */
    @GetMapping("/editTwo/{id}")
    public String editTwo(@PathVariable("id") String id, ModelMap mmap)
    {
        SupplyClass supplyClass = supplyClassService.selectSupplyClassById(id);
        mmap.put("supplyClass", supplyClass);
        return prefix + "/editTwo";
    }
    /**
     * 修改供需类型
     */
    @GetMapping("/editThr/{id}")
    public String editThr(@PathVariable("id") String id, ModelMap mmap)
    {
        SupplyClass supplyClass = supplyClassService.selectSupplyClassById(id);
        mmap.put("supplyClass", supplyClass);
        return prefix + "/editThr";
    }

    /**
     * 修改保存供需类型
     */
    @RequiresPermissions("jiaqin:supplyclass:edit")
    @Log(title = "供需类型", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SupplyClass supplyClass)
    {
        supplyClass.setUpdateAt(new Date());
        return toAjax(supplyClassService.updateSupplyClass(supplyClass));
    }

    /**
     * 删除供需类型
     */
    @RequiresPermissions("jiaqin:supplyclass:remove")
    @Log(title = "供需类型", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(supplyClassService.deleteSupplyClassByIds(ids));
    }
}
